package com.hbx.residential_service.controller;

import com.github.pagehelper.PageInfo;
import com.hbx.residential_service.domain.Admin;
import com.hbx.residential_service.domain.House;
import com.hbx.residential_service.service.HouseService;
import com.hbx.residential_service.utils.DateStringEditor;
import com.hbx.residential_service.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.net.URLDecoder;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("/house")
public class HouseController {
    @Autowired
    private HouseService houseService;
//    @InitBinder
//    public void initBinder(WebDataBinder binder){
//        binder.registerCustomEditor(Date.class,new DateStringEditor());
//    }

//    //查询房屋(无分页)
//    @RequestMapping("/findAll.do")
//    public ModelAndView findAll() throws Exception {
//        ModelAndView mv = new ModelAndView();
//        List<House> hs = houseService.findAll();
//        mv.addObject("houseList",hs);
//        mv.setViewName("house-list");
////        mv.setViewName("test1");
//        return mv;
//    }
    //房屋查询（分页）
    @RequestMapping("/findAll.do")
    public ModelAndView findAll(Model model,HttpSession session, @RequestParam(name="page",required = true,defaultValue = "1")int page, @RequestParam(name = "size",required = true,defaultValue = "5")int size) throws Exception {
        ModelAndView mv = new ModelAndView();
        List<House> hs = houseService.findAll(page,size);
        PageInfo pageInfo = new PageInfo(hs);//重要 是一个分页bean
        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("house-list");
        //session.getAttribute("adminname");
        model.addAttribute("adminname",session.getAttribute("adminname"));
        return mv;
    }

    //添加信息
    @RequestMapping("/save.do")
    public String save(House house) throws Exception {
        houseService.save(house);
        return "redirect:findAll.do";
    }

    //删除房屋信息
    @RequestMapping("/delete.do")
    @ResponseBody
    public ModelAndView deleteHouse(String houseIds) throws Exception{
        //houseService.deleteHouse(houseIds);
        String[] strs = houseIds.split(",");
        for (int i = 0; i < strs.length; i++) {
            try {
                houseService.deleteHouse(Integer.parseInt(strs[i]));
            } catch (Exception e) {
            }
        }
        return new ModelAndView("redirect:findAll.do");
    }

    //查询要修改的房屋信息
    @RequestMapping("/findupdbyid.do")
    public ModelAndView findUpdHouseById(int houseId){
        House house =houseService.findUpdHouseById(houseId);
        ModelAndView mv = new ModelAndView();
        mv.addObject("house",house);
        mv.setViewName("house-update");
        return mv;
    }

    //修改房屋信息
    @RequestMapping("/updbyid.do")
    public String UpdateById(House house){
        houseService.UpdateById(house);
        return "redirect:findAll.do";
    }

    //模糊查询
    @RequestMapping("/fuzzysearchbyusername.do")
    public ModelAndView fsByUsername(Model model,HttpSession session,String content,@RequestParam(name="page",required = true,defaultValue = "1")int page, @RequestParam(name = "size",required = true,defaultValue = "5")int size)throws Exception{
        ModelAndView mv = new ModelAndView();
//        content=URLDecoder.decode(content, "UTF-8").toString();
        content = new String(content.getBytes("iso-8859-1"),"utf-8");
//        TimeUnit.SECONDS.sleep(1);
        List<House> fshl = houseService.fsByUsername(content,page,size);
        PageInfo pageInfo = new PageInfo(fshl);//重要 是一个分页bean
        mv.addObject("pageInfo",pageInfo);
        mv.addObject("content",content);
        mv.setViewName("house-search");
        //session.getAttribute("adminname");
        model.addAttribute("adminname",session.getAttribute("adminname"));
        return mv;
    }
}
